package com.online_video.dao.impl;

import com.online_video.dao.BaseDao;
import com.online_video.dao.VideoDao;
import com.online_video.entity.Collect;
import com.online_video.entity.CollectBook;
import com.online_video.entity.User;
import com.online_video.entity.Video;
import java.util.List;

public class VideoDaoImpl extends BaseDao implements VideoDao {
    @Override
    public int insert(Video video) {
        String sql = "INSERT INTO `T_video` (`vid`,`viName`,`u_id`,`playAmount`,`category`,`like`,`collect`,`date`,`videoPath`,`img`,`describe`) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        return super.update(sql,video.getVid(),video.getViName(),video.getU_id(),video.getPlayAmount(),video.getCategory(),video.getLike(),video.getCollect(),video.getDate(),video.getVideoPath(),video.getImg(),video.getDescribe());
    }

    @Override
    public int update(Video video) {
        return 0;
    }

    @Override
    public int delete(Integer vid) {
        String sql = "DELETE FROM `T_video` WHERE `vid` = ?";
        return super.update(sql,vid);
    }

    /**
     * 由视频的名称进行模糊查询
     * @param viName 视频名称
     * @return
     */
    @Override
    public List<Video> queryByViName(String viName) {
        String sql = "SELECT * FROM `T_video` WHERE `viName` like '%"+viName+"%'";
        return super.queryForList(Video.class,sql);
    }

    /**
     * 由视频的分类进行检索
     * @param category 视频的分类
     * @return
     */
    @Override
    public List<Video> queryByCategory(String category) {
        String sql = "SELECT * FROM `T_video` WHERE category = ? limit 0,8";
        return super.queryForList(Video.class,sql,category);
    }

    /**
     * 由视频id进行检索
     * @param vid
     * @return
     */
    @Override
    public Video queryByVid(Integer vid) {
        String sql = "SELECT * FROM `T_video` WHERE vid = ?";
        return super.queryForOne(Video.class,sql,vid);
    }

    /**
     * 检索当前用户上传的所有视频信息
     * @param u_id 上传视频的用户的id
     * @return
     */
    @Override
    public List<Video> queryByU_id(Integer u_id) {
       String sql ="SELECT * FROM `T_video` WHERE `u_id` = ?";
        return super.queryForList(Video.class,sql,u_id);
    }

    @Override
    public List<CollectBook> queryCollectBookByid(Integer u_id) {
        String sql = "SELECT * FROM `T_collect_book` WHERE `u_id` = ?";
        return super.queryForList(CollectBook.class,sql,u_id);
    }

    @Override
    public int insertCollectBook(CollectBook collectBook) {
        String sql = "INSERT INTO `T_collect_book`(`u_id`,`c_book_name`) VALUES(?,?)";
        return super.update(sql,collectBook.getU_id(),collectBook.getC_book_name());
    }

    @Override
    public CollectBook isExitCollectBookById(Integer u_id,String c_book_name) {
        String sql = "SELECT * FROM `T_collect_book` WHERE `u_id` = ? AND `c_book_name` = ?";
        return super.queryForOne(CollectBook.class,sql,u_id,c_book_name);
    }

    @Override
    public List<CollectBook> queryCollBook(Integer u_id, Integer c_book_id) {
        String sql = "select c_book_name,viName,nickname,category,img,c_date,v.vid,collectid from t_collect_book cb\n" +
                "INNER JOIN t_collect c ON c.c_book_id = cb.c_book_id\n" +
                "INNER JOIN t_video v ON v.vid = c.vid\n" +
                "INNER JOIN t_user u ON u.id = v.u_id\n" +
                "\n" +
                "WHERE cb.u_id = ? AND cb.c_book_id = ?";
        return super.queryForList(CollectBook.class,sql,u_id,c_book_id);
    }

    @Override
    public int insertCollect(Collect collect) {
        String sql = "INSERT INTO `T_collect`(`vid`,`u_id`,`c_book_id`,`c_date`) VALUES(?,?,?,?)";
        return super.update(sql,collect.getVid(),collect.getU_id(),collect.getC_book_id(),collect.getC_date());
    }

    @Override
    public int delCollectVideo(Integer collectid) {
        String sql = "DELETE FROM `T_collect` WHERE `collectid` = ?";
        return super.update(sql,collectid);
    }

    @Override
    public List<Collect> isExitCollectVideo(Integer vid, Integer u_id) {
        String sql = "SELECT * FROM `T_collect` WHERE `vid` = ? AND `u_id` = ?";
        return super.queryForList(Collect.class,sql,vid,u_id);
    }

    @Override
    public int cancelCollectVideo(Integer vid, Integer u_id) {
        String sql = "DELETE FROM `T_collect` WHERE `vid` = ? AND `u_id` = ?";
        return super.update(sql,vid,u_id);
    }

    @Override
    public int collectCount(Integer vid) {
        String sql = "SELECT COUNT(*) FROM `T_collect` WHERE `vid` = ?";
        return Integer.parseInt(String.valueOf(super.queryForSingleValue(sql,vid)));
    }

    @Override
    public Integer queryForPageTotalCount(String parm,String category) {
        String sql = "SELECT COUNT(1) FROM `T_video`";
        if (!("".equals(category)) && !("null").equals(category)){
            sql += " WHERE `category` = '" + category + "'";
        }

        if (!("".equals(parm)) && !("null".equals(parm))){
            sql += " WHERE `viName` LIKE '%"+ parm +"%'";
        }
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Video> queryForPageItems(int begin, int pageSize,String parm,String category) {
        String sqlStart = "SELECT * FROM `T_video`";
        if (!("".equals(category)) && !("null".equals(category))){
            sqlStart += " WHERE `category` = '" + category + "'";
        }
        if (!("".equals(parm)) && !("null".equals(parm))){
            sqlStart += " WHERE `viName` LIKE '%"+ parm +"%'";
        }
        String sqlEnd = sqlStart + " limit ?,?";
        return super.queryForList(Video.class,sqlEnd,begin,pageSize);
    }

    @Override
    public User queryUserByVid(Integer vid) {
        String sql = "select * from t_user u INNER JOIN t_video v on u.id = v.u_id where vid = ?";
        return super.queryForOne(User.class,sql,vid);
    }

}
